#!/bin/sh

LABEL=PR-$1
export LABEL
echo ${LABEL}

cd /root
rm -rf Deployment

unzip -qo /root/ChefDeployment.zip
rm -fr /root/Deployment/.chef
cp -r /root/.chef /root/Deployment

cd /root/Deployment/ruby-gem/depac-common
gem build depac-common.gemspec
gem install depac-common-0.0.1.gem

cd /root/Deployment/ruby-gem/depac-build
gem build depac-build.gemspec
gem install depac-build-0.0.1.gem

cd /root/Deployment/ruby-gem/depac-deployment
gem build depac-deployment.gemspec
gem install depac-deployment-0.0.1.gem

cd /root/Deployment/ruby-gem/depac-automation
gem build depac-automation.gemspec
gem install depac-automation-0.0.1.gem

cd /root/Deployment
cp ruby-gem/depac-build/config/price-build.yml .

if ! provisionBuild -f price-build.yml -l ${LABEL}; then
        echo "provisionBuild failed. Cannot provision soft layer images !" 1>&2
        exit 1
fi
echo "provisionBuild Finished"

echo "Copying gems to deployment-ci VM"

scp /root/ChefDeployment.zip deplci:~/
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "rm -rf Deployment"
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "unzip -qo ChefDeployment.zip"
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "bash --login -c 'cd /root/Deployment/ruby-gem/depac-common; gem build depac-common.gemspec; gem install depac-common-0.0.1.gem; cd /root/Deployment/ruby-gem/depac-deployment; gem build depac-deployment.gemspec; gem install depac-deployment-0.0.1.gem; cd /root/Deployment/ruby-gem/depac-automation; gem build depac-automation.gemspec; gem install depac-automation-0.0.1.gem; cp /root/Deployment/ruby-gem/depac-deployment/config/price-deploy.yml /root/Deployment/'"

echo "Copying gems to deployment-prod VM"

scp /root/ChefDeployment.zip deplprod:~/
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "rm -rf Deployment"
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "unzip -qo ChefDeployment.zip"
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "bash --login -c 'cd /root/Deployment/ruby-gem/depac-common; gem build depac-common.gemspec; gem install depac-common-0.0.1.gem; cd /root/Deployment/ruby-gem/depac-deployment; gem build depac-deployment.gemspec; gem install depac-deployment-0.0.1.gem; cd /root/Deployment/ruby-gem/depac-automation; gem build depac-automation.gemspec; gem install depac-automation-0.0.1.gem; cp /root/Deployment/ruby-gem/depac-deployment/config/price-deploy.yml /root/Deployment/'"

echo "sharing snapshots"
shareBuildSnapshots /root/Deployment/deployment_data/SnapShotReport_${LABEL}.yml dev-ops price-build.yml
echo "Update Env disabled"
echo "Updating the devops qa environment to build ${LABEL}"
ssh -o TCPKeepAlive=yes -o ServerAliveInterval=50 -t -t deplci "bash --login -c '>~/.ssh/known_hosts; cp ~/cloud-deployment-key* ~/Deployment/ws_data/; cd /root/Deployment; updateEnvironment ../DeploymentOutput/QA-PR-20150204-1317.yml ${LABEL}'"
echo "devops qa environment updated"
echo "sharing build snapshot with appops environment"
shareBuildSnapshots /root/Deployment/deployment_data/SnapShotReport_${LABEL}.yml app-ops price-build.yml
